package com.example.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.example.springboot.service.GoodsService;
import com.example.springboot.pojo.Goods;
import com.example.springboot.pojo.Result;
@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping("/create")
    public Result create(@RequestBody Goods goods) {
        goodsService.create(goods);
        return Result.success();
    }
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        goodsService.delete(id);
        return Result.success();
    }
    @GetMapping("/findAll")
    public Result findAll() {
        List<Goods> goodsList = goodsService.findAll();
        return Result.success(goodsList);
    }
    @GetMapping("/findById")
    public Result findById( Integer id) {
        return Result.success(goodsService.findById(id));
    }
    @GetMapping("/findByName")
    public Result findByName(@RequestParam String name) {
        return Result.success(
                goodsService.findByName(name)
        );
    }
    @PostMapping("/update")
    public Result update(@RequestBody Goods goods) {
        goodsService.update(goods);
        return Result.success();
    }

    @GetMapping("/selectPage")
    public Result selectPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(goodsService.selectPage(pageNum, pageSize));
    }
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        goodsService.deleteBatch(ids);
        return Result.success();
    }

}
